package com.aorise.controller.app;

import com.aorise.exception.DataValidationException;
import com.aorise.exception.SystemException;
import com.aorise.model.order.WorkOrderInfo;
import com.aorise.model.order.WorkOrderModel;
import com.aorise.model.system.User;
import com.aorise.service.order.RepairCameraService;
import com.aorise.service.order.WorkOrderService;
import com.aorise.utils.ConstDefine;
import com.aorise.utils.StatusDefine;
import com.aorise.utils.StatusDefineMessage;
import com.aorise.utils.WorkOrderEnum;
import com.aorise.utils.json.JsonResponseData;
import com.aorise.utils.page.Page;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: zhouhao
 * @Date: 2019/6/11
 * @Description:    APP运维工单控制器
 */
@Slf4j
@RestController
@Api(description ="APP运维工单控制器")
@RequestMapping("app/workOrder")
public class AppWorkOrderController {

    @Autowired
    private WorkOrderService workOrderService;

    @Autowired
    private RepairCameraService repairCameraService;

    /**
     * 根据筛选条件进行分页查询运维工单信息
     * HTTP 方式：GET
     * API 路径：/app/workOrder/pageIndex/{pageIndex}/pageNum/{pageNum}
     * 方法名：getAllWorkOrderListByPage
     * 方法返回类型：String
     */
    @ApiOperation(value = "根据筛选条件进行分页查询运维工单信息", httpMethod = "GET", response = String.class, notes = "根据筛选条件进行分页查询运维工单信息,提供运维工单APP-查询列表使用")
    @RequestMapping(value = "/pageIndex/{pageIndex}/pageNum/{pageNum}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getAllWorkOrderListByPage(@ApiParam(value = "页索引", required = true) @PathVariable(value = "pageIndex", required = true) String pageIndex,
                                            @ApiParam(value = "页大小", required = true) @PathVariable(value = "pageNum", required = true) String pageNum) {

        log.debug("Request RESTful API:getAllWorkOrderListByPage");
        log.debug("pageIndex" + pageIndex);
        log.debug("pageNum" + pageNum);

        Page page = new Page();
        if (!StringUtils.isBlank(pageIndex)) {
            page.setCurrentPage(Long.parseLong(pageIndex.toString()));
        }
        if (!StringUtils.isBlank(pageNum)) {
            page.setEveryPage(Long.parseLong(pageNum.toString()));
        }
        try {

            Map<String, Object> map = new HashMap<>();
            map.put("state", ConstDefine.STATE_ABLE);
            map.put("beginIndex", page.getBeginIndex());
            map.put("endinIndex", page.getEndinIndex());
            /**区分PC和APP端*/
            map.put("app", "app");
            map.put("waitStatus", WorkOrderEnum.WAITLIST.getEn());

            List<WorkOrderModel> WorkOrderModels = workOrderService.getAllWorkOrderListByPage(map, page);
            page.setList(WorkOrderModels);

            return new JsonResponseData(true,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS, "", page).toString();
        } catch (DataAccessException e) {
            log.error(
                    "controller:AppWorkOrderController. function:getAllWorkOrderListByPage...msg:Get WorkOrder List By Page DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), page).toString();
        } catch (Exception e) {
            log.error(
                    "controller:AppWorkOrderController. function:getAllWorkOrderListByPage...msg:Get WorkOrder List By Page Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), page).toString();
        }
    }

    /**
     * 运维工单详情
     * HTTP 方式：GET
     * API 路径：/app/workOrder/getWorkOrderInfo
     * 方法名：getWorkOrderInfo
     * 方法返回类型：String
     *
     * @Author: zhouhao
     * @Date: 2019-06-10
     */
    @ApiOperation(value = "运维工单详情", httpMethod = "GET", response = String.class, notes = "运维工单详情，提供运维工单APP使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "工单ID",  paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "/getWorkOrderInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getWorkOrderInfo(Integer orderId) {

        try{
            WorkOrderInfo workOrderInfo =repairCameraService.getWorkOrderInfo(orderId);
            return JsonResponseData.ofSuccess(workOrderInfo);
        }catch (DataValidationException e) {
            log.error(
                    "controller:AppWorkOrderController. function:getWorkOrderInfo...msg: getWorkOrderInfo DataValidationException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        }catch (DataAccessException e) {
            log.error(
                    "controller:AppWorkOrderController. function:getWorkOrderInfo...msg:getWorkOrderInfo DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:AppWorkOrderController. function:getWorkOrderInfo...msg:getWorkOrderInfo Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 修改运维工单状态
     * HTTP 方式：GET
     * API 路径：/app/workOrder/upWorkOderState
     * 方法名：upWorkOderState
     * 方法返回类型：String
     *
     * @Author: zhouhao
     * @Date: 2019-06-10
     */
    @ApiOperation(value = "修改运维工单状态", httpMethod = "PUT", response = String.class, notes = "修改运维工单状态，提供运维工单APP使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "工单ID",  paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "orderState", value = "状态 1改派 2接单 3退回 4 待确认 5 完成",  paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "logDescribe", value = "退回原因,退回的时候传入",  paramType = "query", dataType = "String")

    })
    @RequestMapping(value = "/upWorkOderState", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String upWorkOderState(Integer orderId,Integer orderState,String logDescribe) {

        try{
            Integer loginUserId = ((User) SecurityUtils.getSubject().getPrincipal()).getId();
            if(repairCameraService.upWorkOderstate(orderId,orderState,loginUserId,logDescribe,null)){
                return JsonResponseData.ofSuccess(null);
            }
            return JsonResponseData.ofFailure();
        }catch (SystemException e) {
            log.error(
                    "controller:AppWorkOrderController. function:upWorkOderState...msg: upWorkOderState SystemException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        }catch (DataValidationException e) {
            log.error(
                    "controller:AppWorkOrderController. function:upWorkOderState...msg: upWorkOderState DataValidationException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        }catch (DataAccessException e) {
            log.error(
                    "controller:AppWorkOrderController. function:upWorkOderState...msg:upWorkOderState DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:AppWorkOrderController. function:upWorkOderState...msg:upWorkOderState Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }


}
